package com.app.core.init;

import com.app.kit.IPUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author qiangt
 * @version 1.0
 * @date 2023/10/17 16:39
 * @description: Spring初始化打印自动装配的Bean
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class PrintInitializer implements CommandLineRunner {

    private final ApplicationContext applicationContext;
    private final Environment environment;

    @Override
    public void run(String... args) throws Exception {
        log.info("Spring自动装配的Bean:");
        log.info(String.join("\n", applicationContext.getBeanDefinitionNames()));
        printBanner();
    }

    private void printBanner() {
        String ip = IPUtils.getLocalIp();
        String port = environment.getProperty("server.port");
        String path = this.environment.getProperty("server.servlet.context-path");
        path = StringUtils.isEmpty(path) ? "" : path;
        log.info("\n\t----------------------------------------------------------\n\t" +
                "Application is running! Access URLs:\n\t" +
                "External  : \thttp://" + ip + ":" + port + path + "/\n\t" +
                "Swagger-ui: \thttp://" + ip + ":" + port + path + "/doc.html\n\t" +
                "----------------------------------------------------------");
    }
}
